package com.fzzz.kotlindemo

import com.google.common.base.Objects
import com.google.common.base.Optional
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableMap

/**
 * Created by Shen Chao
 * Created by 2018-12-26
 */
object GuavaDemo1 {

    @JvmStatic
    fun main(args: Array<String>) {
        //        test6();
        demo2()
    }

    private fun demo2() {
        val sum = sum(Optional.fromNullable(3), Optional.absent())
        if (sum.isPresent) {
            println(sum.get())
        } else {
            println("参数错误")
        }
    }

    private fun demo1() {
        //one
        val possible = Optional.fromNullable(5)  //创建允许null值的Optional
        //two
        val nubmerone = 4
        val integerOptional = Optional.of(nubmerone)//若引用为null则快速失败触发java.lang.NullPointerException
        //three
        val nullOptional = Optional.absent<Int>()//创建引用缺失的Optional实例,就是为NULL的

        val list = ImmutableList.of("a", "b", "c")
        val map = ImmutableMap.of("firstName", "John", "lastName", "Doe")
    }

    private fun test1() {
        val possible = Optional.fromNullable(5)  //创建允许null值的Optional
        if (possible.isPresent) {//包含的引用非null的（引用存在），返回true
            println("possible.value：" + possible.get())//包含的引用缺失(null)，则抛出java.lang.IllegalStateException   //5
        } else {
            println("possible is null")
        }
    }

    private fun test2() {
        val possible = Optional.fromNullable<Int>(null)  //创建允许null值的Optional
        if (possible.isPresent) {//包含的引用非null的（引用存在），返回true
            println("possible.value：" + possible.get())//包含的引用缺失(null)，则抛出java.lang.IllegalStateException
        } else {
            println("possible is null") //print
        }
    }

    private fun test3() {
        val optional1 = Optional.absent<Int>()//null就是缺少的意思
        val value = optional1.or(3)
        println("【if optional1 is null,return is 3,others is value】value =" + value!!)    //3

        val optional2 = Optional.of(5)
        val value2 = optional2.or(3)
        println("【if optional2 is null,return is 3,others is value】value =" + value2!!)   //5
    }

    private fun test4() {
        val nulloptional = Optional.absent<Int>()//null就是缺少的意思
        val value1 = nulloptional.orNull()
        println("value1 = " + value1!!)   //null

        val optional = Optional.of(5)
        val value2 = optional.orNull()
        println("value2 = " + value2!!)   //5
    }

    private fun test5() {
        val nulloptional = Optional.absent<Int>()//null就是缺少的意思
        val set1 = nulloptional.asSet()
        println("set1 size = " + set1.size) //0

        val optional = Optional.of(5)
        val set2 = optional.asSet()
        println("set2 size = " + set2.size) //1
    }

    //求和
    private fun sum(a: Optional<Int>, b: Optional<Int>): Optional<Int> {
        return if (a.isPresent && b.isPresent) {
            Optional.of(a.get() + b.get())
        } else Optional.absent()
    }

    private fun test6() {
        Objects.equal("a", "a")     // returns true
        Objects.equal(null, "a")    // returns false
        Objects.equal("a", null)    // returns false
        Objects.equal(null, null)   // returns true
    }

}
